<template>
    <div>
        公主岭 无编码数据: {{ noCodeData.length }}<br>
        公主岭村+编码: {{ codeData.length }}<br>
        village: {{ Object.keys(villageData).length }}<br>
        四平公主岭镇编码: {{ townJsonData.length }}<br>
        villageCode: {{ Object.keys(villageCodeData).length }}<br>
        <button @click="extractAndPrintTownCodes">提取并打印镇编码</button>
    </div>
</template>

<script setup>
import { ref, onMounted, vModelSelect } from 'vue'
import nocode from '../json/公主岭 无编码数据.json'
import code from '../json/公主岭村+编码.json'
import village from '../json/village.json'
import townJson from '../json/四平公主岭镇编码.json'
import villageCode from '../json/villageCode.json'

const noCodeData = ref(nocode)
const codeData = ref(code)
const villageData = ref(village)
const townJsonData = ref(townJson)
const villageCodeData = ref(villageCode)

const villageFun = (villageCode) => {
    var nameRes = "";
    const failRes = [];

    // 检查villageCode的类型并相应处理
    if (Array.isArray(villageCode.value)) {
        // 如果是数组，直接遍历
        villageCode.value.forEach(item => {
            const coordinates = item.coordinates || [];
            // 调换经纬度顺序
            const swappedCoordinates = coordinates.map(point => {
                if (Array.isArray(point) && point.length >= 2) {
                    return [point[1], point[0]]; // 交换经纬度
                }
                return point;
            });

            failRes.push({
                code: item.code,
                name: item?.name,
                boundary: JSON.stringify(swappedCoordinates)
            });
            nameRes += `INSERT INTO area (code, name, level, parent_id) VALUES ('${item.code
                }','${item?.name}','6','${String(item.code).substring(
                    0,
                    9
                )}');`;
        });
    } else if (typeof villageCode.value === 'object' && villageCode.value !== null) {
        // 如果是对象，使用Object.values转为数组再遍历
        Object.values(villageCode.value).forEach(item => {
            const coordinates = item.coordinates || [];
            // 调换经纬度顺序
            const swappedCoordinates = coordinates.map(point => {
                if (Array.isArray(point) && point.length >= 2) {
                    return [point[1], point[0]]; // 交换经纬度
                }
                return point;
            });

            failRes.push({
                code: item.code,
                name: item?.name,
                boundary: JSON.stringify(swappedCoordinates)
            });
            nameRes += `INSERT INTO area (code, name, level, parent_id) VALUES ('${item.code
                }','${item?.name}','6','${String(item.code).substring(
                    0,
                    9
                )}');`;
        });
    } else {
        console.error('错误: villageCode不是数组或对象');
        return;
    }

    console.log("sql", nameRes);
    console.log("json", failRes);
}

// 提取townJson中的code并用逗号隔开打印
const extractAndPrintTownCodes = () => {
    // 过滤掉undefined的code
    const codes = townJson
        .filter(item => item.code !== 'undefined')
        .map(item => item.code)

    // 用逗号连接所有code
    const codesString = codes.join(',')

    // 打印结果
    console.log('镇编码列表:')
    console.log(codesString)

    return codesString
}

// 页面加载时自动执行一次
onMounted(() => {
    villageFun(villageCodeData)
    // extractAndPrintTownCodes()

    // 检查village.json的格式并打印详细信息
    console.log('village.json格式检查:')
    console.log('类型:', typeof villageData.value)
    console.log('是否为对象:', Object.prototype.toString.call(villageData.value) === '[object Object]')
    console.log('键的数量:', Object.keys(villageData.value).length)

    // 检查第一个条目
    // const firstKey = Object.keys(villageData.value)[0]
    // if (firstKey) {
    //     console.log('第一个条目:', {
    //         key: firstKey,
    //         value: villageData.value[firstKey]
    //     })
    // }
})
</script>

<style lang="scss" scoped></style>